import { Article, History, SearchAction, Suggestion } from "@/types/store";
import { getLocalHistories } from "@/utils/storage";
type SearchState = {
  suggestion: Suggestion,
  histories: History,
  // 存放搜索的结果
  results: Article[],
  resLength: number
}

const initialValue: SearchState = {
  suggestion: [],
  histories: getLocalHistories() || [],
  // 存放搜索的结果
  results: [],
  resLength: 0
}
export default function search(state = initialValue, action: SearchAction): SearchState {
  switch (action.type) {
    case 'search/suggestion':
      return {
        ...state,
        suggestion: action.payload
      }
    case 'search/saveHistories':
      return {
        ...state,
        histories: action.payload
      }
    case 'search/clearHistories':
      return {
        ...state,
        histories: [],
      }
    case 'search/replaceHistories':
      return {
        ...state,
        histories: action.payload
      }
    case 'search/saveResults':
      return {
        ...state,
        results: [
          ...state.results,
          ...action.payload,
        ],
        resLength: action.payload.length
      }
    case 'search/clearArticle':
      return {
        ...state,
        results: [],
      }
    default:
      return state
  }
}